//
//  QZChooseView.m
//  map
//
//  Created by 000 on 16/11/2.
//  Copyright © 2016年 faner. All rights reserved.
//

#import "QZChooseView.h"
#import "constant.h"
//#import <BmobSDK/BmobObject.h>
#import "BmobSDK/Bmob.h"
#import "UIView+Extestion.h"
#import "QZTableViewCell.h"
@interface QZChooseView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *orderArray;//第几条线数组
    NSMutableArray *firstLineNameA;
    NSMutableArray *groupCountArray;//组中的数量
    NSUserDefaults *userDefaults1;
}
@property (nonatomic, strong) NSMutableArray *selectedArray;//被选中的数组
@property (weak, nonatomic) IBOutlet UITableView *chosseTableView;
@property (weak, nonatomic) IBOutlet UIButton *seletedB;
@property (nonatomic, strong) NSMutableArray *needloadCountArray;//需要传递值的数组
- (IBAction)checkedBC:(id)sender;
- (IBAction)backBC:(id)sender;
- (IBAction)selectedBC:(id)sender;

@end

@implementation QZChooseView

- (NSMutableArray *)needloadCountArray
{
    if (_needloadCountArray == nil) {
        _needloadCountArray = [NSMutableArray array];
    }
    return _needloadCountArray;
}

- (NSMutableArray *)selectedArray
{
    if (_selectedArray == nil) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}

+ (instancetype)chooseViewWithFrame:(CGRect)frame
{
    QZChooseView *chooseView = [[NSBundle mainBundle] loadNibNamed:@"QZChooseView" owner:nil options:nil].lastObject;
    chooseView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    chooseView.frame = frame;
    return chooseView;
}
- (void)loadThirdOrderData
{
    BmobQuery *firstQuerry = [BmobQuery queryWithClassName:@"thirdOrder"];
    [firstQuerry whereKey:@"loginUser" equalTo:[userDefaults1 objectForKey:@"detailSave"]];
    [firstQuerry orderByAscending:@"createdAt"];
    [firstQuerry calcInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        orderArray = [NSMutableArray array];
        firstLineNameA = [NSMutableArray array];
        groupCountArray = [NSMutableArray array];
        for (BmobObject *obj in array) {
            
            NSString *groupCountStr = [obj objectForKey:@"groupCountStr"];
            
            NSString *lineName = [obj objectForKey:@"lineName"];
            NSString *cacheStr = [obj objectForKey:@"cacheStr"];
            [orderArray addObject:cacheStr];
            [groupCountArray addObject:groupCountStr];
            [firstLineNameA addObject:lineName];
        }
        [self.chosseTableView reloadData];
    }];
}

- (void)awakeFromNib
{
    
    userDefaults1 = [NSUserDefaults standardUserDefaults];

    [self loadThirdOrderData];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hide];
}

- (void)show
{
    //获取主Window
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    self.alpha = 0.0f;
    [UIView animateWithDuration:0.3f animations:^{
        self.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        self.alpha = 1.0f;
    } completion:^(BOOL finished) {
    }];
}
- (void)hide
{
    [self removeFromSuperview];
    [UIView animateWithDuration:0.3f animations:^{
        self.frame=CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        nil;
    }];
}

- (IBAction)backBC:(id)sender
{
    [self hide];
}
#pragma mark UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return firstLineNameA.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = firstLineNameA[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.indexPath = (int)indexPath.row;
    NSString *groupCountStr = [NSString stringWithFormat:@"%d",[groupCountArray[indexPath.row] intValue]];
    [self.needloadCountArray addObject:groupCountStr];
    NSString *selectStr = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    [self.selectedArray addObject:selectStr];
    
    
    NSLog(@"%@",self.needloadCountArray);
    NSLog(@"%@",self.selectedArray);
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *deleteStr = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    [self.selectedArray removeObject:deleteStr];
    NSString *groupCountStr = [NSString stringWithFormat:@"%d",[groupCountArray[indexPath.row] intValue]];
    [self.needloadCountArray removeObject:groupCountStr];
    
    
    
    NSLog(@"%@",self.needloadCountArray);
    NSLog(@"%@",self.selectedArray);
}


- (IBAction)selectedBC:(id)sender {
    if ([self.delegate respondsToSelector:@selector(chooseView:didDoneBtnClickWithSelectedArray:groupCountArray:)]) {
        [self.delegate chooseView:self didDoneBtnClickWithSelectedArray:self.selectedArray groupCountArray:self.needloadCountArray];
    }

    [self.selectedArray removeAllObjects];
    [self.needloadCountArray removeAllObjects];
}

- (IBAction)checkedBC:(id)sender {
    //支持同时选中多行
    self.chosseTableView.allowsMultipleSelectionDuringEditing = YES;
    
    [self.chosseTableView setEditing:!self.chosseTableView.editing animated:YES];

    
}
@end
